package com.hdyg.sixg.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.hdyg.sixg.R;
import com.hdyg.sixg.activity.trans.BuyActivity;
import com.hdyg.sixg.activity.trans.BuyDetailActivity;
import com.hdyg.sixg.activity.trans.SellDetailActivity;
import com.hdyg.sixg.adapter.TransChildAdapter;
import com.hdyg.sixg.entry.OrderBean;
import com.hdyg.sixg.mvp.Contrant.CFragTransChild;
import com.hdyg.sixg.mvp.base.BasePresenter;
import com.hdyg.sixg.mvp.base.BaseUrl;
import com.hdyg.sixg.mvp.base.GetParamUtil;
import com.hdyg.sixg.mvp.base.SpMsg;
import com.hdyg.sixg.mvp.presenter.PFragTransChild;
import com.hdyg.sixg.ui.CustomLoadMoreView;
import com.hdyg.sixg.util.DataCenter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.List;

import butterknife.BindView;


/**
 * @author CZB
 * @describe 交易中心子fragment
 * @time 2019/4/3 17:07
 */
public class TransChildFragment extends BaseFragment implements CFragTransChild.IVFragTransChild {

    public final static int ORDER_ALL = 1;      //全部
    public final static int ORDER_HANG_UP = 2;  //我的挂售
    public final static int ORDER_BUY = 3;      //我的购买
    public final static int ORDER_SELL = 4;     //我的出售
    @BindView(R.id.rv_child)
    RecyclerView rvChild;
    @BindView(R.id.srl_refresh)
    SmartRefreshLayout srlRefresh;


    private final static int REQUEST_CODE = 0x00001;
    private int TYPE;
    private TransChildAdapter mAdapter;
    private CFragTransChild.IPFragTransChild mPresenter;
    private int page = 1, allDataSize,clickPos;
    private List<OrderBean.ListBean> orderBeans;

    /**
     * @param type 类型
     * @return
     */
    public static TransChildFragment newInstance(int type) {
        TransChildFragment fragment = new TransChildFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void createPresenter() {
        mPresenter = new PFragTransChild(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_trans_child;
    }

    @Override
    protected void initView() {
        TYPE = getArguments().getInt("type", ORDER_ALL);
        rvChild.setLayoutManager(new LinearLayoutManager(mContext));
        onrefresh();
        initAdapter();
    }

    //刷新
    private void onrefresh() {
        srlRefresh.setOnRefreshListener(refreshLayout -> {
            srlRefresh.finishRefresh(2000);//传入false表示刷新失败
            page = 1;
            initData();
        });
    }

    @Override
    protected void initData() {
        if (TYPE == ORDER_ALL) {
            mPresenter.pGetOrder(BaseUrl.ALL_ORDER_URL, GetParamUtil.getAllOrderParam(page));
        } else if (TYPE == ORDER_HANG_UP) {
            mPresenter.pGetOrder(BaseUrl.HANG_UP_ORDER_URL, GetParamUtil.getHandUpOrderParam(page));
        } else if (TYPE == ORDER_BUY) {
            mPresenter.pGetOrder(BaseUrl.BUY_ORDER_URL, GetParamUtil.getBuyOrderParam(page));
        } else if (TYPE == ORDER_SELL) {
            mPresenter.pGetOrder(BaseUrl.SELL_ORDER_URL, GetParamUtil.getSellOrderParam(page));
        }
    }

    private void initAdapter() {
        mAdapter = new TransChildAdapter(R.layout.item_fragment_trans, orderBeans, TYPE);
        mAdapter.setLoadMoreView(new CustomLoadMoreView());
        rvChild.setAdapter(mAdapter);
        mAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            int id = view.getId();
            clickPos = position;
            if (id == R.id.bt_buy) {
                mBundle = new Bundle();
                if (TYPE == ORDER_ALL) {
                    //全部订单
                    mBundle.putString(SpMsg.INTENT_STR, mAdapter.getData().get(position).getId());
                    startActivityForResult(BuyActivity.class, mBundle,REQUEST_CODE);
                } else if (TYPE == ORDER_HANG_UP) {
                    //我的挂售
                    if ("2".equals(mAdapter.getData().get(position).getStatus())) {
//                        toastNotifyShort("执行下架操作");
                        mPresenter.pGetDownSell(BaseUrl.DOWN_SELL_URL,GetParamUtil.getDownSellParam(mAdapter.getData().get(position).getId()));
                    }
                } else if (TYPE == ORDER_BUY) {
                    //我的购买
                    mBundle.putString(SpMsg.INTENT_STR, mAdapter.getData().get(position).getId());
                    startActivity(BuyDetailActivity.class, mBundle);
                } else if (TYPE == ORDER_SELL) {
                    //我的出售
                    mBundle.putString(SpMsg.INTENT_STR, mAdapter.getData().get(position).getId());
                    startActivity(SellDetailActivity.class, mBundle);
                }

            }
        });
        mAdapter.setOnLoadMoreListener(() -> {
            if (mAdapter.getData().size() < allDataSize) {
                page++;
                initData();
            } else {
                mAdapter.loadMoreEnd();
            }
        }, rvChild);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != Activity.RESULT_OK){
            return;
        }
        switch (requestCode){
            case REQUEST_CODE:
                page = 1;
                initData();
                break;
        }
    }

    @Override
    public void vGetOrderSuccess(OrderBean dataBean) {
        orderBeans = dataBean.getList();
        allDataSize = dataBean.getTotal();//总条数
        if (page == 1) {
            if (mAdapter != null) {
                mAdapter.setNewData(orderBeans);
            } else {
                initAdapter();
            }
        } else {
            if (orderBeans.size() != 0) {
                mAdapter.addData(orderBeans);
            }
            mAdapter.loadMoreComplete();
        }
    }

    @Override
    public void vGetDownSellSuccess(String dataBean) {
        toastNotifyShort(dataBean);
        OrderBean.ListBean item = mAdapter.getData().get(clickPos);
        item.setStatus("1");
        mAdapter.setData(clickPos,item);
    }
}
